﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HCQ2_Common.Constant;
using HCQ2_Model.ViewModel.WebAPI;

namespace HCQ2_Model.Data
{
    /// <summary>
    /// 业务操作返回结果集泛型
    /// </summary>
    /// <typeparam name="TData"></typeparam>
    public class OperationResult<TData>
    {
        /// <summary>
        /// 是否成功；true成功、false失败
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 获取或设置 返回代码
        /// </summary>
        public Nullable<int> Code { get; set; }

        /// <summary>
        /// 获取或设置 返回消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 获取或设置 结果数据
        /// </summary>
        public TData Data { get; set; }

        /// <summary>
        /// 初始化一个实例
        /// </summary>
        /// <param name="success"></param>
        public OperationResult(bool success)
        {
            this.Success = success;
            this.Message = "操作成功";
        }
        /// <summary>
        /// 初始化一个实例
        /// </summary>
        /// <param name="success"></param>
        /// <param name="message"></param>
        public OperationResult(bool success, string message)
        {
            this.Success = success;
            this.Message = message;
        }
        /// <summary>
        /// 初始化一个实例
        /// </summary>
        /// <param name="success"></param>
        /// <param name="message"></param>
        /// <param name="data"></param>
        public OperationResult(bool success, string message, TData data)
        {
            this.Success = success;
            this.Message = message;
            this.Data = data;
        }

        /// <summary>
        /// 初始化一个实例
        /// </summary>
        /// <param name="success"></param>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <param name="code"></param>
        public OperationResult(bool success, string message, TData data,Nullable<int> code)
        {
            this.Success = success;
            this.Message = message;
            this.Data = data;
            this.Code = code;
        }

        /// <summary>
        /// 结果集转换为接口返回数据格式
        /// </summary>
        /// <returns></returns>
        public WebApiResultJsonModel ToWebApiResultJsonModel()
        {
            WebApiResultJsonModel webApiResult = new WebApiResultJsonModel();
            //如果成功代码：0,如果失败并且没有设置Code属性，那么代码为：101否则是Code
            webApiResult.errcode = Success ? WebResultCode.Ok : Code == null ? WebResultCode.Exception : Code.Value;
            webApiResult.errmsg = Message;
            webApiResult.value = Data;

            return webApiResult;
        }

        /// <summary>
        /// 结果集转换为接口返回的泛型数据格式
        /// </summary>
        /// <returns></returns>
        public WebApiResultJsonModel<TData> ToWebApiResultJsonModelTData()
        {
            WebApiResultJsonModel<TData> webApiResult = new WebApiResultJsonModel<TData>();
            //如果成功代码：0,如果失败并且没有设置Code属性，那么代码为：101否则是Code
            webApiResult.errcode = Success ? WebResultCode.Ok : Code == null ? WebResultCode.Exception : Code.Value;
            webApiResult.errmsg = Message;
            webApiResult.value = Data;

            return webApiResult;
        }
    }

    /// <summary>
    /// 业务操作返回结果集
    /// </summary>
    public class OperationResult : OperationResult<object>
    {
        /// <summary>
        /// 初始化一个实例
        /// </summary>
        /// <param name="s"></param>
        public OperationResult(bool s)
            : base(s)
        {}
        /// <summary>
        /// 初始化一个实例
        /// </summary>
        /// <param name="success"></param>
        /// <param name="message"></param>
        public OperationResult(bool success, string message)
            : base(success, message)
        { }
        /// <summary>
        /// 初始化一个实例
        /// </summary>
        /// <param name="success"></param>
        /// <param name="message"></param>
        /// <param name="data"></param>
        public OperationResult(bool success, string message, object data)
            : base(success, message, data)
        { }
        /// <summary>
        /// 初始化一个实例
        /// </summary>
        /// <param name="success"></param>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <param name="code"></param>
        public OperationResult(bool success, string message, object data, Nullable<int> code)
            : base(success, message, data, code)
        { }
    }
}
